let logger = require("pomelo-logger").getLogger(__filename);
let COMPONENT_NAME = "currencyComponent";

exports.create = function (owner) {
    return new CurrencyComponent(owner);
};

let CurrencyComponent = function (owner) {
    this.owner = owner;
    this.diamond = 0;
    this.gold = 0;
    this.fangka = 0;
    this.dailyRecharge = 0;
    this.totalRecharge = 0;
    this.lastLoginTime = 0;
};

CurrencyComponent.prototype.toJSON = function () {
    return {
        "diamond": this.diamond,
        "dailyRecharge": this.dailyRecharge,
        "totalRecharge": this.totalRecharge,
        "lastLoginTime": this.lastLoginTime,
        "gold": this.gold,
        "fangka": this.fangka
    };
};

CurrencyComponent.prototype.getDiamond = function () {
    return this.diamond;
};

CurrencyComponent.prototype.changeDiamond = function (diamond) {
    let value = +diamond;
    if (!isNaN(value)) {
        this.diamond += value;
    } else {
        logger.warn('changeDiamond diamond not valid:' + diamond);
    }
};

CurrencyComponent.prototype.getGold = function () {
    return this.gold;
};

CurrencyComponent.prototype.changeGold = function (gold) {
    let value = +gold;
    if (!isNaN(value)) {
        this.gold += value;
    } else {
        logger.warn('changeGold gold not valid:' + gold);
    }
};

CurrencyComponent.prototype.getFangka = function () {
    return this.fangka;
};

CurrencyComponent.prototype.changeFangka = function (fangka) {
    let value = +fangka;
    if (!isNaN(value)) {
        this.fangka += value;
    } else {
        logger.warn('changeFangka fangka not valid:' + fangka);
    }
};

CurrencyComponent.prototype.getTotalRecharge = function () {
    return this.totalRecharge;
};

CurrencyComponent.prototype.addTotalRecharge = function (recharge) {
    this.totalRecharge += + recharge;
};

CurrencyComponent.prototype.getDailyRecharge = function () {
    return this.dailyRecharge;
};

CurrencyComponent.prototype.changeDailyRecharge = function (recharge) {
    this.dailyRecharge = + recharge;
};

CurrencyComponent.prototype.getLastLoginTime= function () {
    return this.lastLoginTime;
};

CurrencyComponent.prototype.changeLastLoginTime= function (lastLoginTime) {
    this.lastLoginTime = lastLoginTime;
};

CurrencyComponent.prototype.initWyw = function () {
    this.gold = 40000;
};

CurrencyComponent.prototype.fromBSON = function (document) {
    if (!document.hasOwnProperty(COMPONENT_NAME)) {
        logger.error("load currencyComponent failed. document=" + JSON.stringify(document));
        return;
    }
    let node = document[COMPONENT_NAME];
    this.diamond = + (node["diamond"]) || 0;
    this.dailyRecharge = + (node["dailyRecharge"]) || 0;
    this.totalRecharge = + (node["totalRecharge"]) || 0;
    this.lastLoginTime = + (node["lastLoginTime"]);
    this.gold = + (node["gold"]) || 0;
    this.fangka = + (node["fangka"]) || 0;
};

CurrencyComponent.prototype.buildBSON = function (document) {
    document[COMPONENT_NAME] = {
        diamond : this.diamond,
        dailyRecharge : this.dailyRecharge,
        totalRecharge : this.totalRecharge,
        lastLoginTime : this.lastLoginTime,
        gold : this.gold,
        fangka : this.fangka
    };
};
